﻿using UnityEngine;
using UnityEditor;
using System.Collections.Generic;
using Antlr.StringTemplate;
using System.IO;


public class CSMonoBehaviourGen : EditorWindow {
    private string className = "";
    GUILayoutOption[] nmopt = { GUILayout.Width(175) };

    [MenuItem("Assets/Create/C# MonoBehaviour")]
    static void Init()
    {
        // Get existing open window or if none, make a new one:
        CSMonoBehaviourGen window = (CSMonoBehaviourGen)EditorWindow
            .GetWindow(typeof(CSMonoBehaviourGen));
        window.Show();
    }

    public CSMonoBehaviourGen() {}

    void OnGUI()
    {
        className = EditorGUILayout.TextField("Component name", className, nmopt);

        if (GUILayout.Button("Generate")) {
            StreamReader sr = File.OpenText(
                RueexPaths.paths[(int)RueexPaths.AvailablePaths.Templates]
                + "CSMonoBehaviour.stg");
            StringTemplateGroup stg = new StringTemplateGroup(sr);
            Generate(stg, RueexPaths.paths[(int)RueexPaths.AvailablePaths.Scripts]);

            sr.Close();
        }
    }

    void Generate(StringTemplateGroup stg, string path) {
        if (!Directory.Exists(path))
            Directory.CreateDirectory(path);

        if (File.Exists(path + className + ".cs"))
            return;

        StringTemplate st = stg.GetInstanceOf("behaviour");

        st.SetAttribute("name", className);

        File.WriteAllText(path + className + ".cs",
            st.ToString());
    }
}
